/*
 * ============================================================================
 *
 *  [THC RPG] Total HardCore RPG
 *
 *  File:          upgradesystem.inc
 *  Type:          Core
 *  Description:   contains Upgrade specific functions
 *
 *  Copyright (C) 2009-2010  ArsiRC
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * ============================================================================
 */

#pragma semicolon 1

#define UPGRADE_OFFSET_LEVEL 4
#define UPGRADE_OFFSET_COST 5

PurchaseUpgrade(client,Module:upgrade)
{
    if(client)
    {
        new isbot=IsFakeClient(client);
        if(g_CoreConfigCache[CoreConfig_bot_enable]||!g_CoreConfigCache[CoreConfig_bot_enable]&&!isbot)
        {
            if(g_CoreConfigCache[CoreConfig_bot_control]&&isbot)
            {
                new count=CountHumans();
                if(!count)
                    return -1;
            }
            decl String:name[64];
            GetUpgradeConfigName(upgrade,name,sizeof(name));
            new level=GetPlayerUpgradeLevel(client,upgrade)+1;
            if(level<=GetUpgradeConfigMaxLevel(upgrade))
            {
                new credits=GetCredits(client);
                new cost=GetPlayerUpgradeCost(client,upgrade);
                if(credits>=cost)
                {
                    SetPlayerUpgradeLevel(client,upgrade,level);
                    SetPlayerUpgradeCost(client,upgrade,GetPlayerUpgradeCost(client,upgrade)+GetUpgradeConfigiCost(upgrade));
                    SetCredits(client,GetCredits(client)-cost);
                    TransMgr_PrintText(client, MsgFormat_Plugin, MsgType_Chat, INVALID_MODULE, false, "Successfully purchase upgrade", name, level);
                    PlayPurchaseSound(client);

                    // Call forward
                    new any:eventdata[2][1];
                    eventdata[0][0] = client;
                    eventdata[1][0] = level;
                    new any:modules[2];
                    modules[0] = upgrade;
                    modules[1] = INVALID_MODULE;
                    EventMgr_Forward(g_EvOnUpgradePurchase, eventdata, sizeof(eventdata), sizeof(eventdata[]), g_CommonDataType3, modules);                    
                    return 1;
                }
                else
                    TransMgr_PrintText(client, MsgFormat_Plugin, MsgType_Chat, INVALID_MODULE, false, "Not enough credits for upgrade", name, level, cost);
            }
            else
                TransMgr_PrintText(client, MsgFormat_Plugin, MsgType_Chat, INVALID_MODULE, false, "At max level for upgrade", client);

            PlayUpgradeErrorSound(client);
            return -1;
        }
        else
            return 0;
    }
    else
        return -1;
}

SellUpgrade(client,Module:upgrade)
{
    if(client)
    {
        new isbot=IsFakeClient(client);
        if(g_CoreConfigCache[CoreConfig_bot_enable]||!g_CoreConfigCache[CoreConfig_bot_enable]&&!isbot)
        {
            if(g_CoreConfigCache[CoreConfig_bot_control]&&isbot)
            {
                new count=CountHumans();
                if(!count)
                    return -1;
            }
            decl String:name[64];
            GetUpgradeConfigName(upgrade,name,sizeof(name));
            new level=GetPlayerUpgradeLevel(client,upgrade);
            if(level>0)
            {
                new refund=GetPlayerUpgradeSellPrice(client,upgrade);
                SetCredits(client,GetCredits(client)+refund);
                SetPlayerUpgradeLevel(client,upgrade,level-1);
                SetPlayerUpgradeCost(client,upgrade,GetPlayerUpgradeCost(client,upgrade)-GetUpgradeConfigiCost(upgrade));
                TransMgr_PrintText(client, MsgFormat_Plugin, MsgType_Chat, INVALID_MODULE, false, "Sell upgrade", name, level, refund);
                PlaySellSound(client);

                // Call forward
                new any:eventdata[2][1];
                eventdata[0][0] = client;
                eventdata[1][0] = level-1;
                new any:modules[2];
                modules[0] = upgrade;
                modules[1] = INVALID_MODULE;
                EventMgr_Forward(g_EvOnUpgradeSell, eventdata, sizeof(eventdata), sizeof(eventdata[]), g_CommonDataType3, modules);                    
                return 1;
            }
            else
                return -1;
        }
        else
            return 0;
    }
    else
        return -1;
}

BotBuyRandomUpgrade(client)
{
    if(client)
    {
        new isbot=IsFakeClient(client);
        if(g_CoreConfigCache[CoreConfig_bot_enable]&&isbot==1)
        {
            if(g_CoreConfigCache[CoreConfig_bot_control])
            {
                new count=CountHumans();
                if(!count)
                    return -1;
            }
            for(new moduleindex=1;moduleindex<MODULE_COUNT;moduleindex++)
            {
                new Module:upgrade = Module:moduleindex;
                if(GetCredits(client)>=GetPlayerUpgradeCost(client,upgrade)&&GetPlayerUpgradeLevel(client,upgrade)<GetUpgradeConfigMaxLevel(upgrade))
                {
                    PurchaseUpgrade(client,upgrade);
                    break;
                }
            }
            return 1;
        }
        else
            return 0;
    }
    else
        return -1;
}

SetPlayerUpgradeLevel(client,Module:upgrade,level)
{
    if(client)
    {
        if(level>GetUpgradeConfigMaxLevel(upgrade))
            level=GetUpgradeConfigMaxLevel(upgrade);
        new vecPosi = GetPlayerVectorPosition(client);
        if(vecPosi!=-1)
        {
            SetArrayCell(GetArrayCell(vecPlayers,vecPosi),_:upgrade*2+UPGRADE_OFFSET_LEVEL,level);
            return 1;
        }
        else
            return -1;
    }
    else
        return -1;
}

GetPlayerUpgradeLevel(client,Module:upgrade)
{
    if(client)
    {
        new vecPosi = GetPlayerVectorPosition(client);
        if(vecPosi!=-1)
            return GetArrayCell(GetArrayCell(vecPlayers,vecPosi),_:upgrade*2+UPGRADE_OFFSET_LEVEL);
        else
            return -1;
    }
    else
        return -1;
}

SetPlayerUpgradeCost(client,Module:upgrade,cost)
{
    if(client)
    {
        new vecPosi = GetPlayerVectorPosition(client);
        if(vecPosi!=-1)
        {
            SetArrayCell(GetArrayCell(vecPlayers,vecPosi),_:upgrade*2+UPGRADE_OFFSET_COST,cost);
            return 1;
        }
        else
            return -1;
    }
    else
        return -1;
}

GetPlayerUpgradeCost(client,Module:upgrade)
{
    if(client)
    {
        new vecPosi = GetPlayerVectorPosition(client);
        if(vecPosi!=-1)
            return GetArrayCell(GetArrayCell(vecPlayers,vecPosi),_:upgrade*2+UPGRADE_OFFSET_COST);
        else
            return -1;
    }
    else
        return -1;
}

GetPlayerUpgradeCount(client)
{
    if(client)
    {
        new count=0;
        for(new moduleindex=1;moduleindex<MODULE_COUNT;moduleindex++)
        {
            new Module:upgrade = Module:moduleindex;
            if(GetPlayerUpgradeLevel(client,upgrade)>0)
                count++;
        }
        return count;
    }
    else
        return -1;
}

GetPlayerUpgradeSellPrice(client,Module:upgrade)
{
    if(client)
        return RoundToNearest((GetUpgradeConfigiCost(upgrade)*(GetPlayerUpgradeLevel(client,upgrade)-1)+GetUpgradeConfigCost(upgrade))*g_CoreConfigCache[CoreConfig_save_percent]);
    else
        return -1;
}

GetUpgradeConfigName(Module:upgrade,String:name[],maxlen)
{
    ModuleMgr_ReadString(upgrade, ModuleData_FullName, name, maxlen);
}

stock GetUpgradeConfigShortName(Module:upgrade,String:shortname[],maxlen)
{
    ModuleMgr_ReadString(upgrade, ModuleData_ShortName, shortname, maxlen);
}

GetUpgradeConfigTeamLock(Module:upgrade)
{
    return ModuleMgr_ReadCell(upgrade, ModuleData_TeamLock);
}

GetUpgradeConfigMaxLevel(Module:upgrade)
{
    return ModuleMgr_ReadCell(upgrade, ModuleData_MaxLevel);
}

GetUpgradeConfigCost(Module:upgrade)
{
    return ModuleMgr_ReadCell(upgrade, ModuleData_Cost);
}

GetUpgradeConfigiCost(Module:upgrade)
{
    return ModuleMgr_ReadCell(upgrade, ModuleData_iCost);
}

bool:IsUpgradeMaxed(client,Module:upgrade)
{
    if(client)
        return (GetPlayerUpgradeLevel(client,upgrade)>=GetUpgradeConfigMaxLevel(upgrade));
    else
        return true;
}
